<% set_page_title(:time_tracking) %>

<%= turbo_stream_from :time_entries %>

<% if params[:new_entry].present? %>
  <%
    new_entry = TimeEntry.new
    new_entry.project = Project.find(params[:new_entry][:project_id]) if params[:new_entry][:project_id].present?
    new_entry.issue = new_entry.project.issues.find_by_id(params[:new_entry][:issue_id]) if params[:new_entry][:issue_id].present?
    new_entry.reference_date = @reference_date
  %>

  <%= render partial: 'form', locals: { time_entry: new_entry } %>
<% elsif params[:time_entry_id].present? %>
  <%= render partial: 'form', locals: { time_entry: TimeEntry.find(params[:time_entry_id]) } %>
<% else %>
  <%= start_pending_app_tour_tag("time_entries/index") %>

  <%= turbo_frame_tag 'time_entry_form' do %>
  <% end %>
<% end %>

<%= turbo_frame_tag 'time_entries' do %>
  <div class="flex flex-col md:flex-row justify-stretch items-stretch sm:justify-between gap-4 mb-6">
    <%= form_with(url: time_entries_path, method: :get,
      class: 'flex items-stretch justify-between flex-row-reverse sm:flex-row gap-4',
      data: { turbo_method: :get, turbo_frame: 'time_entries',
      controller: 'form'
    } ) do |f| %>

      <label class="input max-w-36">
        <i class="ti ti-calendar opacity-50"></i>

        <input id="go-to-date"
          name="reference_date"
          class="grow"
          placeholder="Reference Date"
          data-controller="flatpickr"
          data-action="change->form#submit"
          data-flatpickr-min-date="2019-01-01"
          data-flatpickr-max-date="2030-01-01"
          value="<%= l @reference_date, format: :flatpickr %>" type="text" readonly="readonly">
      </label>

      <% if @reference_date != Date.current %>
        <%= link_to time_entries_path(reference_date: Date.current.strftime("%Y-%m-%d")),
            class: 'btn btn-ghost',
            data: { 'turbo-action': 'advance' } do %>
            <%= t(".goto_today") %>
        <% end %>
      <% end %>


    <% end %>
    <%= link_to new_time_entry_path(reference_date: @reference_date),
        class: "btn btn-primary tour--new-time-entry",
        data: { turbo_frame: 'time_entry_form' } do %>
      <%= icon_for(:add_entry) %>
      <%= "#{t('actions.add')} #{TimeEntry.model_name.human.downcase}" %>
    <% end %>
  </div>

  <% if @remaining_dates_with_time_entry_running.any? %>
    <%= render AlertComponent.new(message: t(".other_time_entries_running")) do |component| %>
      <% @remaining_dates_with_time_entry_running.each do |date| %>
        <% component.with_action do %>
          <%= link_to time_entries_path(reference_date: date.strftime("%Y-%m-%d")), class: "btn btn-primary btn-soft flex-nowrap", data: { 'turbo-action': 'advance' } do %>
            <span><%= l(date, format: :short).downcase %></span>
            <i class="ti ti-arrow-right"></i>
          <% end %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>

  <div class="mb-8 mt-4 flex gap-4 justify-stretch overflow-x-auto items-stretch text-nowrap tour--calendar-dates">
    <% @calendar_dates.each do |date| %>
      <%
        wrap_tag = (date == @reference_date) ? :div : :a
        wrap_options = if (date == @reference_date)
          {
            class: "bg-accent text-accent-content"
          }
        else
          {
            class: " ",
            href: time_entries_path(reference_date: date)
          }
        end
        wrap_options[:data] = {
          'reference-date': date.strftime("%Y-%m-%d"),
          'turbo-action': 'advance'
        }
        wrap_options[:class] += " min-w-32 md:min-w-auto transition-colors hover:bg-accent hover:text-accent-content grow rounded-box flex flex-col items-center justify-center px-2 py-4"

      %>

      <%= content_tag wrap_tag, wrap_options do %>
        <span class="text-sm opacity-80 font-semibold"><%= l(date, format: :short_weekday_name )%>, <%= l(date, format: :short )%></span>
        <span class="text-sm opacity-70 font-medium mt-2 turbo-stream-logged-time-text"><%= convert_minutes_to_human_readable_hour(@total_logged_time_per_day[date] || 0) %></span>
      <% end %>
    <% end %>
  </div>

  <template id="js-time-entry-demo">
    <div class="tour--temporary-element mb-4">
      <%= render partial: 'time_entry', locals: { time_entry: TimeEntry.new(id: 'temp-demo-entry', project: Project.new(id: 'temp-demo-project', name: 'Example time entry'), description: 'Example time entry description', reference_date: @reference_date) } %>
    </div>
  </template>

  <div class="flex flex-col gap-8" id="time-entries-tbody">
    <%= render partial: 'time_entry', collection: @time_entries, as: :time_entry %>

    <%= render EmptyListComponent.new(
      message: t(".zero_records", date: l(@reference_date, format: :long )),
      action: {
        url: new_time_entry_path(reference_date: @reference_date),
        text: t('.click_here_to_create'),
        extra_options: { data: { turbo_frame: 'time_entry_form' } }
      })
    %>

  </div>
<% end %>
